package view
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.media.*;
    import flash.text.*;

    import mx.core.SoundAsset;

    import model.GameModelLocator;
    import model.vo.GameVo;
    import view.*;

    public class GameScreen extends Sprite
    {
        public static var SPEED_MIN:int = 2;
        public static var SPEED_MAX:int = 32;
        public static var DISTANCE:int = 50000;
        [Embed(source='sounds/gamePlay.mp3')]
        public static var gamePlay:Class;
        public static var playMusic:SoundAsset = new gamePlay() as SoundAsset;
        public static var musicChannel:SoundChannel;
        
        public var gameVo:GameVo = GameModelLocator.getInstance().gameVo;
        public var background:Background = new Background();
        public var people:People = new People();
        public var itemArray:Array = new Array();

        private var text:TextField = new TextField();
        private var speed:int;
        private var distance:int;
        private var eggPoint:int;
        private var frameCount:int;

        public function GameScreen()
        {
            this.addChild(this.background);
            this.addChild(this.people);
            this.text.x = 300;
            this.text.y = 10;
            this.text.thickness = 2;
            this.text.text = "Distance:  " + this.distance.toString() +
                             "      Speed:  " + speed.toString();
            this.text.textColor = 0xFFFFFF;
            this.text.width = 200;
            this.text.height = 40;
            this.addChild(this.text);
            this.addEventListener(Event.ENTER_FRAME, this.update);
        }

        public function reset():void
        {
            this.distance = 0;
            this.eggPoint = 0;
            this.speed = 8;
            this.frameCount = 0;
            if(GameScreen.musicChannel != null)
            {
                GameScreen.musicChannel.stop();
            }
            GameScreen.musicChannel = GameScreen.playMusic.play();
        }

        public function display():void
        {
            this.graphics.clear();
            this.graphics.beginFill(0x00A0FF, 1);
            this.graphics.drawRect(0, 0, 520, 440);
            this.graphics.endFill();
            this.background.display();
            this.people.display();
            this.text.text = "Distance:  " + this.distance.toString() +
                             "      Speed:  " + this.speed.toString();
        }

        private function speedUp():void
        {
            if(this.speed + 2 <= GameScreen.SPEED_MAX)
            {
                this.speed += 2;
            }
        }

        private function speedDown():void
        {
            if(this.speed - 4 >= GameScreen.SPEED_MIN)
            {
                this.speed -= 4;
            }
            else
            {
                this.speed = GameScreen.SPEED_MIN;
            }
        }

        private function collisionDetection(item:Item):Boolean
        {
            if((this.people.people.x < item.x) &&
               (item.x + item.width < this.people.people.x + this.people.people.width) &&
               (this.people.people.y < item.y) &&
               (item.y + item.height < this.people.people.y + this.people.people.height))
            {
                return true;
            }
            return false;
        }

        private function updateItems():void
        {
            var i:int=0;
            while(i<this.itemArray.length)
            {
                this.itemArray[i].move(this.speed);
                if(this.itemArray[i].x < -this.itemArray[i].width)
                {
                    this.removeChild(this.itemArray[i]);
                    this.itemArray.splice(i, 1);
                }
                else if(this.collisionDetection(this.itemArray[i]))
                {
                    if(this.itemArray[i].type == Item.TYPE_EGG)
                    {
                        this.gameVo.score += 100;
                    }
                    else if(this.itemArray[i].type == Item.TYPE_UP)
                    {
                        this.speedUp();
                    }
                    else
                    {
                        this.speedDown();
                    }
                    this.removeChild(this.itemArray[i]);
                    this.itemArray.splice(i, 1);
                }
                else
                {
                    this.itemArray[i].display();
                    i ++;
                }
            }
            for(i=0; i<this.speed; i++)
            {
                this.createItems();
            }
            if(this.gameVo.state == Constants.GAME_STATE.RUNNING &&
               this.eggPoint > GameScreen.DISTANCE/10 &&
               this.gameVo.eggs < 5)
            {
                this.eggPoint -= GameScreen.DISTANCE/10;
                this.createEgg();
            }
        }

        private function createEgg():void
        {
            var item:Item = new Item(Item.TYPE_EGG, 500, 10 + 400 * Math.random());
            this.addChild(item);
            this.itemArray.push(item);
            item.display();
        }

        private function createItems():void
        {
            if(this.gameVo.state == Constants.GAME_STATE.RUNNING)
            {
                var i:int = 1000 * Math.random();
                var t:int = 0;
                while(i >= this.gameVo.missionRules[t])
                {
                    i -= this.gameVo.missionRules[t];
                    t ++;
                }
                if(t != Item.TYPE_EMPTY)
                {
                    var item:Item = new Item(t, 500, 10 + 400 * Math.random());
                    this.addChild(item);
                    this.itemArray.push(item);
                    item.display();
                }
            }
        }

        private function update(e:Event):void
        {
            this.background.move(this.speed);
            this.people.move(this.speed);
            this.updateItems();
            this.distance += this.speed;
            this.eggPoint += this.speed;
            this.display();
            this.frameCount ++;
            if(this.distance > GameScreen.DISTANCE)
            {
                this.gameVo.arrive = true;
            }
        }
    }
}
